package com.tarena.lbs.user.service.impl;

import com.github.pagehelper.PageInfo;
import com.tarena.lbs.base.common.utils.Asserts;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.common.enums.PassportResultEnum;
import com.tarena.lbs.common.principle.UserPrinciple;
import com.tarena.lbs.common.utils.AuthenticationContextUtils;
import com.tarena.lbs.common.utils.PageConverter;
import com.tarena.lbs.user.dao.repository.UserRepository;
import com.tarena.lbs.user.dao.repository.UserTagsRepository;
import com.tarena.lbs.user.enums.UserResultEnum;
import com.tarena.lbs.user.pojo.param.UserParam;
import com.tarena.lbs.user.pojo.param.UserTagsParam;
import com.tarena.lbs.user.pojo.po.UserPO;
import com.tarena.lbs.user.pojo.po.UserTagsPO;
import com.tarena.lbs.user.pojo.query.UserQuery;
import com.tarena.lbs.user.pojo.vo.UserVO;
import com.tarena.lbs.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private UserTagsRepository userTagsRepository;

    @Override
    public void reg(UserParam param) throws BusinessException {
        log.info("用户注册业务处理:{}", param);
        int num = userRepository.isExist(param.getPhone());
        Asserts.isTrue(num>0,new BusinessException(UserResultEnum.PHONE_EXISTS));
        UserPO userPO = new UserPO();
        userPO.setUnionId(UUID.randomUUID().toString());
        userPO.setRegTime(new Date());
        userPO.setStatus(0);
        BeanUtils.copyProperties(param, userPO);
        userRepository.add(userPO);
    }

    @Override
    public UserVO detail() throws BusinessException {
        UserPrinciple userPrinciple = AuthenticationContextUtils.get();
        if (userPrinciple == null) throw new BusinessException(UserResultEnum.USER_NOT_LOGIN);
        Integer id = userPrinciple.getId();
        if (id == null) throw new BusinessException(UserResultEnum.USER_NOT_LOGIN);
        log.info("用户详情:{}", id);
        UserPO userPO = userRepository.detail(id);
        Asserts.isTrue(userPO == null, new BusinessException(PassportResultEnum.USER_NONE_EXIST));
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(userPO, userVO);
        return userVO;
    }

    @Override
    public void bindUserTags(UserTagsParam param) throws BusinessException {
        log.info("用户id与用户标签绑定参数{}",param);
        Integer userId = param.getUserId();
        String tagIds = param.getTagIds();
        UserPO user = userRepository.getById(userId);
        if (user == null){
            throw new BusinessException(PassportResultEnum.USER_NONE_EXIST);
        }
        userTagsRepository.removeUserTags(userId);
        String[] split = tagIds.split(",");
        List<UserTagsPO> userTagsPOList = new ArrayList<>();
        for (String tagId : split) {
            UserTagsPO userTagPO = new UserTagsPO();
            userTagPO.setUserId(userId);
            userTagPO.setTagId(Integer.parseInt(tagId));
            userTagsPOList.add(userTagPO);
        }
        userTagsRepository.bindUserTags(userTagsPOList);
    }

    @Override
    public PageResult<UserVO> showList(UserQuery userQuery) {
        PageInfo<UserPO> pageInfo = userRepository.showList(userQuery);
        List<UserVO> userVOList = pageInfo.getList().stream().map(userPO -> {
            UserVO userVO = new UserVO();
            userVO.setUserNickPicture(userPO.getUserImage());
            BeanUtils.copyProperties(userPO, userVO);
            return userVO;
        }).collect(Collectors.toList());
        log.info("用户列表{}",pageInfo);
        return PageConverter.convert(pageInfo,userVOList);
    }
}
